﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.Company;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCompanyList();
            if (ddlCompany.Items.Count > 0)
            {
                if (Session[Global.CompanyId] != null)
                {
                    ddlCompany.Text = Session[Global.CompanyId].ToString();
                }
                else
                {
                    Session[Global.CompanyId] = ddlCompany.Text;
                    Session[Global.CompanyName] = ddlCompany.SelectedItem.Text;
                }
            }
        }
    }

    private void BindCompanyList()
    {
        CompanyProfileList _profileList = new CompanyProfileList();
        if (Cache[Global.CompanyListCacheId] != null)
        {
            _profileList = (CompanyProfileList)Cache[Global.CompanyListCacheId];
        }
        else
        {
            CompanyProfileDB _profileDB = new CompanyProfileDB();
            _profileList = _profileDB.GetProfileList();
            if (_profileList != null && _profileList.Count > 0)
            {
                Cache[Global.CompanyListCacheId] = _profileList;
            }
        }

        if (_profileList != null && _profileList.Count > 0)
        {
            ddlCompany.Items.Clear();
            foreach (CompanyProfile p in _profileList)
            {
                ddlCompany.Items.Add(new ListItem(p.CompanyName, p.Id.ToString()));
            }
        }
    }


    protected void PagePreRender(object sender, EventArgs e)
    {

    }


    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Entity/Default.aspx");
    }

    protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (ddlCompany.Items.Count > 0)
        {
            Session[Global.CompanyId] = ddlCompany.Text;
            Session[Global.CompanyName] = ddlCompany.SelectedItem.Text;
        }
        else
        {
            Session[Global.CompanyId] = "0";
            Session[Global.CompanyName] = string.Empty;
        }

        Response.Redirect(Request.Url.ToString());
    }
}
